<template>
  <a-modal
    :open="open"
    :title="$t('menu.selectIcon')"
    :width="800"
    :footer="null"
    @cancel="handleCancel"
  >
    <a-tabs v-model:activeKey="activeKey">
      <a-tab-pane key="outlined" :tab="$t('menu.icon.outlined')">
        <div class="icon-container">
          <div 
            v-for="icon in outlinedIcons"
            :key="icon"
            class="icon-item"
            :class="{ 'icon-selected': selectedIcon === icon }"
            @click="handleIconSelect(icon)"
          >
            <component :is="icon" />
            <div class="icon-name">{{ icon }}</div>
          </div>
        </div>
      </a-tab-pane>
      <a-tab-pane key="filled" :tab="$t('menu.icon.filled')">
        <div class="icon-container">
          <div 
            v-for="icon in filledIcons"
            :key="icon"
            class="icon-item"
            :class="{ 'icon-selected': selectedIcon === icon }"
            @click="handleIconSelect(icon)"
          >
            <component :is="icon" />
            <div class="icon-name">{{ icon }}</div>
          </div>
        </div>
      </a-tab-pane>
      <a-tab-pane key="twoTone" :tab="$t('menu.icon.twoTone')">
        <div class="icon-container">
          <div 
            v-for="icon in twoToneIcons"
            :key="icon"
            class="icon-item"
            :class="{ 'icon-selected': selectedIcon === icon }"
            @click="handleIconSelect(icon)"
          >
            <component :is="icon" />
            <div class="icon-name">{{ icon }}</div>
          </div>
        </div>
      </a-tab-pane>
    </a-tabs>
    <div class="action-container">
      <a-button @click="handleCancel">{{ $t('common.cancel') }}</a-button>
      <a-button type="primary" @click="handleSubmit" :disabled="!selectedIcon">
        {{ $t('common.confirm') }}
      </a-button>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import * as Icons from '@ant-design/icons-vue'

// 组件名称
defineOptions({
  name: 'IconSelectorModal'
})

// Props定义
const props = defineProps<{
  open: boolean
  modelValue?: string
}>()

// Emits定义
const emit = defineEmits<{
  (e: 'update:open', value: boolean): void
  (e: 'update:modelValue', value: string): void
}>()

// 状态管理
const activeKey = ref<string>('outlined')
const selectedIcon = ref<string>(props.modelValue || '')

// 图标分组
const outlinedIcons = Object.keys(Icons).filter(key => key.endsWith('Outlined'))
const filledIcons = Object.keys(Icons).filter(key => key.endsWith('Filled'))
const twoToneIcons = Object.keys(Icons).filter(key => key.endsWith('TwoTone'))

/**
 * 处理图标选择
 * @param icon 选中的图标名称
 */
const handleIconSelect = (icon: string) => {
  selectedIcon.value = icon
}

/**
 * 处理取消操作
 */
const handleCancel = () => {
  emit('update:open', false)
}

/**
 * 处理提交操作
 */
const handleSubmit = () => {
  if (selectedIcon.value) {
    emit('update:modelValue', selectedIcon.value)
    emit('update:open', false)
  }
}

// 监听外部modelValue变化
watch(() => props.modelValue, (val) => {
  if (val) {
    selectedIcon.value = val
  }
})

// 监听打开状态变化
watch(() => props.open, (val) => {
  if (val && !selectedIcon.value && props.modelValue) {
    selectedIcon.value = props.modelValue
  }
})
</script>

<style scoped>
.icon-container {
  display: flex;
  flex-wrap: wrap;
  max-height: 350px;
  overflow-y: auto;
}

.icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100px;
  height: 80px;
  margin: 5px;
  padding: 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.icon-item:hover {
  background-color: #f5f5f5;
}

.icon-name {
  margin-top: 5px;
  font-size: 12px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
}

.icon-selected {
  background-color: #e6f7ff;
  border: 1px solid #1890ff;
}

.action-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
  gap: 8px;
}
</style> 